package org.cainiao.io.nio.buffer;

import java.nio.CharBuffer;

public class BufferFillDrain {

    private int index = 0;

    private final String[] strings = {
        "A random string value",
        "The product of an infinite number of monkeys",
        "Hey hey we're the Monkees",
        "Opening act for the Monkees: Jimi Hendrix",
        "'Scuse me while I kiss this fly", // Sorry Jimi ;-)
        "Help Me! Help Me!",
    };

    public boolean fillBuffer(CharBuffer buffer) {
        if (index >= strings.length) {
            return false;
        }
        buffer.put(strings[index++]);
        return true;
    }

    public void drainBuffer(CharBuffer buffer) {
        while (buffer.hasRemaining()) {
            System.out.print(buffer.get());
        }
        System.out.println();
    }

}
